<template>
  <div class="search-form">
    <!-- 头部tab切换 -->
    <el-row type="flex" class="search-tab">
      <span
        v-for="(item, index) in tabs"
        :key="index"
        @click="handleSearchTab(index)"
        :class="{ active: index === currentTab }"
      >
        <i :class="item.icon"></i>{{ item.name }}
      </span>
    </el-row>
    <el-form class="search-form-content" ref="form" label-width="80px">
      <el-form-item label="出发城市">
        <!-- fetch-suggestions 返回输入建议的方法 -->
        <!-- select 点击选中建议项时触发 -->
        <el-autocomplete
          :fetch-suggestions="queryDepartSearch"
          placeholder="请搜索出发城市"
          @select="handleDepartSelect"
          class="el-autocomplete"
          v-model="airInfo.departCity"
          :trigger-on-focus="false"
          :highlight-first-item="true"
        ></el-autocomplete>
      </el-form-item>
      <el-form-item label="到达城市">
        <el-autocomplete
          :fetch-suggestions="queryDestSearch"
          placeholder="请搜索到达城市"
          @select="handleDestSelect"
          class="el-autocomplete"
          v-model="airInfo.destCity"
          :trigger-on-focus="false"
          :highlight-first-item="true"
        ></el-autocomplete>
      </el-form-item>
      <el-form-item label="出发时间">
        <!-- change 用户确认选择日期时触发 -->
        <el-date-picker
          type="date"
          placeholder="请选择日期"
          style="width: 100%"
          v-model="airInfo.departDate"
          value-format="yyyy-MM-dd"
          :picker-options="{ disabledDate }"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item label="">
        <el-button
          style="width: 100%"
          type="primary"
          icon="el-icon-search"
          @click="handleSubmit"
        >
          搜索
        </el-button>
      </el-form-item>
      <div class="reverse">
        <span @click="handleReverse">换</span>
      </div>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      currentTab: 0, //当前标签
      // tab栏
      tabs: [
        //
        {
          name: "单程",
          icon: "iconfont icondancheng",
        },
        {
          name: "往返",
          icon: "iconfont iconshuangxiang",
        },
      ],
      // 输入框绑定数据
      airInfo: {
        departCity: "",
        departCode: "",
        destCity: "",
        destCode: "",
        departDate: "",
      },
    };
  },
  methods: {
    // 日期可选操作
    disabledDate(time) {
      // console.log(time);
      // now()方法返回自1970年1月1日00:00:00 UTC以来的毫秒数。

      return time.getTime() + 3600 * 1000 * 24 < Date.now();
    },
    // 机票城市请求封装
    getCities(value) {
      return this.$axios({
        url: "/airs/city",
        params: { name: value },
      }).then((res) => {
        console.log(res);
        // **必须有value才可以显示出来**  数据需改造
        // filter:过滤掉没有city.sort的元素
        // map：添加value属性
        let cities = res.data.data
          .filter((city) => {
            return city.sort;
          })
          .map((city) => {
            return {
              ...city,
              value: city.name.replace("市", ""),
            };
          });
        console.log(cities);
        return cities.length == 0 ? [{ value: "没有搜索的城市" }] : cities;
      });
    },
    //   点击切换tab栏
    handleSearchTab(index) {
      if (index == 1) {
        this.$message("暂时只支持单程机票搜索");
        return;
      }
      // this.currentTab = index;
    },
    //出发城市输入框获得焦点时触发
    // value:当前选中的值
    // callback：回调函数，接收要展示的列表
    //   返回输入建议的方法
    queryDepartSearch(value, callback) {
      // let value = this.getCities(value);
      console.log(this.getCities(value)); //获取到的promise对象通过.then(res=>{}) 获取结果
      this.getCities(value).then((res) => {
        // 搜索得出建议选项的时候,默认将列表的第一项 code 数据放入 form
        // 默认给一个值, 如果用户自己选了, 再去覆盖
        // 默认将第一项的代码放入 form 表单当中
        // 不用担心用户之后的操作,
        // 如果用户不选择, 默认就有 code
        // 如果用户选择一个城市,
        // 会后续触发 select 事件
        // 用户选择的数据会覆盖掉默认数据
        this.airInfo.departCode = res[0].sort;
        callback(res);
      });
      // callback(this.getCities(value));
    },
    // 点击选中建议项时触发
    // 出发城市下拉选择时触发
    handleDepartSelect(item) {
      console.log(item);
      this.airInfo.departCode = item.sort;
    },
    // 目标城市输入框获得焦点时触发
    // value 是选中的值，cb是回调函数，接收要展示的列表
    queryDestSearch(value, callback) {
      this.getCities(value).then((res) => {
        this.airInfo.destCode = res[0].sort;
        callback(res);
      });
    },
    // 目标城市下拉选择时触发
    handleDestSelect(item) {
      console.log(item);
      this.airInfo.destCode = item.sort;
    },
    // 出发时间
    // 用户确认选择日期时触发
    // handleDate() {},
    // 和目标城市切换时触发
    handleReverse() {
      console.log(this.airInfo);
      const { departCity, departCode, destCity, destCode } = this.airInfo;
      this.airInfo.departCity = destCity;
      this.airInfo.departCode = destCode;
      this.airInfo.destCity = departCity;
      this.airInfo.destCode = departCode;
      console.log(this.airInfo);
    },
    // 提交表单是触发
    handleSubmit() {
      this.airInfo.departCity = this.airInfo.departCity.replace("市", "");
      this.airInfo.destCity = this.airInfo.destCity.replace("市", "");
      console.log(this.airInfo);
      // 跳转到飞机列表页之前,先将信息存储到仓库
      this.$store.commit("historystore/addHistory", this.airInfo);
      this.$router.push({
        path: "/air/flights",
        query: this.airInfo,
      });
    },
  },
};
</script>

<style scoped lang="less">
.search-form {
  border: 1px #ddd solid;
  border-top: none;
  width: 360px;
  height: 350px;
  box-sizing: border-box;
}

.search-tab {
  span {
    display: block;
    flex: 1;
    text-align: center;
    height: 48px;
    line-height: 42px;
    box-sizing: border-box;
    border-top: 3px #eee solid;
    background: #eee;
  }

  .active {
    border-top-color: orange;
    background: #fff;
  }

  i {
    margin-right: 5px;
    font-size: 18px;

    &:first-child {
      font-size: 16px;
    }
  }
}

.search-form-content {
  padding: 15px 50px 15px 15px;
  position: relative;

  .el-autocomplete {
    width: 100%;
  }
}

.reverse {
  position: absolute;
  top: 35px;
  right: 15px;

  &:after,
  &:before {
    display: block;
    content: "";
    position: absolute;
    left: -35px;
    width: 25px;
    height: 1px;
    background: #ccc;
  }

  &:after {
    top: 0;
  }

  &:before {
    top: 60px;
  }

  span {
    display: block;
    position: absolute;
    top: 20px;
    right: 0;
    font-size: 12px;
    background: #999;
    color: #fff;
    width: 20px;
    height: 20px;
    line-height: 18px;
    text-align: center;
    border-radius: 2px;
    cursor: pointer;

    &:after,
    &:before {
      display: block;
      content: "";
      position: absolute;
      left: 10px;
      width: 1px;
      height: 20px;
      background: #ccc;
    }

    &:after {
      top: -20px;
    }

    &:before {
      top: 20px;
    }
  }
}
</style>